package org.lex.swing.prefs;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;

import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;

import org.lex.swing.widgets.GradientPaintLabel2;
import org.lex.utils.text.Splitter;


public class TitleDecorator extends CategoryDecorator {
	private static final TitleDecorator[] EMPTY = new TitleDecorator[0];
	private JLabel titleLabel = null;
	private JPanel titlePane = null;
	private JPanel view = null;

	public static <T extends Category> TitleDecorator[] decorate(T... categories) {
		if (null == categories || 0 == categories.length)
			return EMPTY;
		TitleDecorator[] tds = new TitleDecorator[categories.length];
		for (int i = 0; i < tds.length; i++)
			tds[i] = new TitleDecorator(categories[i]);
		return tds;
	}

	public TitleDecorator(Category decoratee) {
		super(decoratee);
		this.initialize();
	}

	public Component getCategoryComponent() {
		return this.view;
	}

	private String formatTitle(String title) {
		return this.lastTitle(title);
	}

	/*
	 * return: "Dot.Splitted.Title" -> "Title"
	 */
	String lastTitle(String title) {
		return title.substring(title.lastIndexOf('.') + 1);
	}

	/*
	 * produce: "Dot.Splitted.Title" -> "Dot<sep>Splitted<sep>Title"
	 */
	String fullTitle(String title, String sep) {
		StringBuilder buf = new StringBuilder();
		buf.append(' ');
		String[] tokens = Splitter.DotSplitter.split(title);
		for (int i = 0; i < tokens.length; i++) {
			buf.append(tokens[i]);
			if (i < tokens.length - 1)
				buf.append(sep);
		}
		return buf.toString();
	}

	private void initialize() {
		this.view = new JPanel(new BorderLayout(0, 0));
		view.add(getTitlePane(), BorderLayout.NORTH);
		view.add(getDecorateeComponent(), BorderLayout.CENTER);
	}

	private JPanel getTitlePane() {
		if (titlePane == null) {
			titleLabel = new GradientPaintLabel2(new Color(118, 161, 222), new Color(63, 102, 188));
			Font f = titleLabel.getFont();
			f = f.deriveFont(Font.BOLD | Font.ITALIC, f.getSize2D() + 15);
			titleLabel.setFont(f);
			titleLabel.setText(" " + this.formatTitle(this.getTitle()));
			titleLabel.setForeground(Color.white);
			titlePane = new JPanel();
			titlePane.setLayout(new BorderLayout());
			titlePane.add(titleLabel, BorderLayout.NORTH);
			titlePane.add(new JSeparator(), BorderLayout.SOUTH);
		}
		return titlePane;
	}

	private Border border = null;

	private Component getDecorateeComponent() {
		if (null == border) {
			// scrollPane = new
			// JScrollPane(getDecoratee().getCategoryComponent());
			// scrollPane.setBorder( BorderFactory.createCompoundBorder(
			// BorderFactory
			// .createEtchedBorder( EtchedBorder.LOWERED ),
			// BorderFactory.createEmptyBorder( 0,
			// 12, 0, 0 ) ) );
			// border = BorderFactory.createCompoundBorder( BorderFactory
			// .createEtchedBorder( EtchedBorder.LOWERED ),
			// BorderFactory.createEmptyBorder( 0,
			// 11, 0, 0 ) );
			border = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
			Component c = this.getDecoratee().getCategoryComponent();
			if (c instanceof JComponent)
				((JComponent) c).setBorder(border);
		}
		return this.getDecoratee().getCategoryComponent();
	}
}
